<?php
class Product_model extends CI_Model {
	public function getById($lang = 'vi', $id) {
		$data = array ();
		$this->db->select ( 'p.id, p.name, pl.product_description,pl.short_desc' );
		$this->db->from ( 'products p' );
		$this->db->where ( 'p.public', '1' );
		$this->db->where ( 'p.id', $id );
		/*$this->db->order_by ( 'p.order' );*/
		/*	$this->db->join('photo ph','ph.product_id = p.id');*/
		$this->db->join ( 'product_languages pl', 'p.id = pl.product_id' );
		$this->db->join ( 'languages l', 'l.id = pl.language_id AND l.code =\'' . $lang . '\'' );
		
		$q = $this->db->get ();
		
		if ($q->num_rows ()) {
			$data = $q->row_array ();
		}
		$q->free_result ();
		return $data;
	}
	public function getProByName($lang = 'en', $name) {
		$data = array ();
		$this->db->select ( 'p.id, pl.product_description,p.name,pl.short_desc' );
		$this->db->from ( 'products p' );
		$this->db->where ( 'p.public', '1' );
		$this->db->where ( 'p.name', $name );
		/*$this->db->order_by ( 'p.order' );*/
		/*	$this->db->join('photo ph','ph.product_id = p.id');*/
		$this->db->join ( 'product_languages pl', 'p.id = pl.product_id' );
		$this->db->join ( 'languages l', 'l.id = pl.language_id AND l.code =\'' . $lang . '\'' );
		
		$q = $this->db->get ();
		/*$q = $this->db->get('products');*/
		
		if ($q->num_rows ()) {
			$data = $q->row_array ();
		}
		$q->free_result ();
		return $data;
	}
	public function getByChildCatId($id) {
		$data = array ();
		$this->db->select ( 'p.id, p.name' );
		$this->db->from ( 'products p' );
		$this->db->where ( 'p.category_id', $id );
		$this->db->where ( 'p.public', '1' );
		/*$this->db->where ( 'c.category_id', NULL );*/
		$this->db->order_by ( 'p.order' );
		//$this->db->join ( 'categories c', 'p.category_id = c.id' );
		

		$q = $this->db->get ();
		
		if ($q->num_rows ()) {
			$data = $q->result_array ();
		}
		$q->free_result ();
		return $data;
	}
	public function getallPro($lang) {
		$data = array ();
		$this->db->select('p.sku,p.name,p.price,pl.short_desc,p.id');
		$this->db->from('products p');
		$this->db->where('p.public','1');
		$this->db->join('product_languages pl','p.id = pl.product_id');
			$this->db->join ( 'languages l', 'l.id = pl.language_id AND l.code =\'' . $lang . '\'' );
		$q = $this->db->get();
		if ($q->num_rows ()) {
			$data = $q->result_array ();
		}
		$q->free_result ();
		return $data;
	}
	
	public function insertProduct() {
		$data = array ('sku' => $_GET ['sku'], 'name' => $_GET ['name'], 'price' => $_GET ['price'], 'image' => $_GET ['image'], 'quantity' => $_GET ['quantity'], 'category_id' => $_GET ['cate_id'], 'manufacture_id' => '1', 'order' => '1', 'public' => '1' );
		
		$q = $this->db->insert ( 'products', $data );
		
		$this->db->select ( 'max(id)' );
		$this->db->from ( 'products' );
		$q2 = $this->db->get ();
		if ($q2->num_rows ()) {
			$data = $q2->row_array ();
		}
		$data = array ('product_id' => $data ['max(id)'], 'language_id' => $_GET ['lang'], 'product_description' => $_GET ['MyTextarea'] );
		
		$q1 = $this->db->insert ( 'product_languages', $data );
		/*$q1 = $this->db->get_where('product_languages',array('product_id' => mysql_insert_id()));*/
	}
	public function getFourNewProduct() {
		$data = array ();
		$this->db->select ( 'name , image' );
		$this->db->from ( 'products' );
		$this->db->where('public','1');
		$this->db->order_by ( 'id', 'desc' );
		$this->db->limit ( 4 );
		$q = $this->db->get ();
		if ($q->num_rows ()) {
			$data = $q->result_array ();
		}
		$q->free_result ();
		return $data;
	}

}